package com.syl.dao.impl;

import com.syl.dao.UserDao;
import com.syl.entity.User;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * --- 就那样---
 *
 * @author SYL
 * @date 2025/8/1
 * @desc
 */
public class UserDaoImpl implements UserDao {


    // 加载驱动不再需要
    // 通过驱动管理器获得连接
    String user1="root";
    String pwd="123456";
    String url="jdbc:mysql://localhost:3306/db_syl?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC";

    static {
        try {

            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }





    @Override
    public User login(String username, String password) throws ClassNotFoundException, SQLException {
        System.out.println("dao层处理");


        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user1, pwd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //创建工具
        // 创建sql执行器
        QueryRunner queryRunner = new QueryRunner();
        //查询
        String sql="select * from tb_user where username=? and password=?";

        /**
         * 参数1: 数据库连接对象
         * 参数2: sql语句字符串,也需要参数变?
         * 参数3: 给?赋值
         */
        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);

        User query= queryRunner.query(connection, sql, userBeanHandler, username,password);


        System.out.println("dao层处理完成 返回对象"+query);

        DbUtils.closeQuietly(connection);


        return query;
    }

    @Override
    public User checkUser(String username) {
        System.out.println("dao层处理");
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user1, pwd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //创建工具
        // 创建sql执行器
        QueryRunner queryRunner = new QueryRunner();
        //查询
        String sql="select * from tb_user where username=?";

        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);

        User query= null;
        try {
            query = queryRunner.query(connection, sql, userBeanHandler, username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        System.out.println("dao层处理完成 返回对象"+query);

        DbUtils.closeQuietly(connection);


        return query;
    }

    @Override
    public boolean register(User user) {
        System.out.println("dao层处理注册用户");
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user1, pwd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //创建工具
        // 创建sql执行器
        QueryRunner queryRunner = new QueryRunner();
        //查询
        String sql="insert into tb_user ( username,password,phone,create_time,sex) values (?,?,?,?,?);";



        int query= 0;
        try {
             query = queryRunner.update(
                                        connection,
                                        sql,
                                        user.getUsername(),
                                        user.getPassword(),
                                        user.getPhone(),
                                        user.getCreateTime(),
                                        user.getSex()
            );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        System.out.println("dao层处理完成 返回对象"+query);


        DbUtils.closeQuietly(connection);

        return query>0;

    }

    @Override
    public List<User> getAllUser() {
        System.out.println("dao层处理查询所有用户");
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user1, pwd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        QueryRunner queryRunner = new QueryRunner();
        String sql="select * from tb_user;";
        BeanListHandler<User> userBeanListHandler = new BeanListHandler<>(User.class);
        List<User> arrUser=null;
        try {
            arrUser= queryRunner.query(connection, sql, userBeanListHandler);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println("dao层处理完成，返回用户列表，共" + arrUser.size() + "条记录");

        DbUtils.closeQuietly(connection);
        return arrUser;
    }

    @Override
    public boolean delectUser(int id) {
        System.out.println("dao层处理注册用户");
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user1, pwd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //创建工具
        // 创建sql执行器
        QueryRunner queryRunner = new QueryRunner();
        //查询
        String sql="delete from tb_user where id = ?";



        int query= 0;
        try {
            query = queryRunner.update(connection, sql, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        System.out.println("dao层处理完成 返回对象"+query);


        DbUtils.closeQuietly(connection);

        return query>0;

    }
}
